!function (angular) {
    const m = angular.module('ngTableFlow', ['ui.bootstrap']);
    m.controller('tableCtrl', function($scope,$uibModal) {
        $scope.list = window.list;
        // $scope.open = function(){
        //     // const modalInstance = $uibModal.open({
        //     //     animation:false,
        //     //     windowClass:'colored-header colored-header-primary',
        //     //     backdropClass:'fade show',
        //     //     templateUrl:'modal.add',
        //     //     controller:'modalInstanceCtrl',
        //     //     controllerAs:'$ctrl'
        //     // });
        // };
    });
    m.filter('toString',function () {
        return function (input) {
            return `${input}`;
        }
    });
    m.filter('none',function () {
        return function (input,out) {
            if(!input){
                return out;
            }
            return input;
        }
    });
    m.controller('modalInstanceCtrl',function ($uibModalInstance,$scope,form) {
        // console.log();
        const $ctrl = this;
        // if (data.userLevel) {
        //     data.userLevel = `${data.userLevel}`;
        // }
        // data.parent
        const {service,data} = form;
        $scope.data = data;
        $scope.service = service;
        console.log('弹窗控制器',$scope,service);
        $ctrl.cancel = function () {
            console.log('取消？');
            $uibModalInstance.dismiss('cancel');
        };

    });
    m.controller('formCtrl',function ($scope,$element) {
        const formEl = $($element);
        formEl.parsley();
        const {service,$ctrl} = $scope;
        console.log('formCtrl',$ctrl);
        $scope.submit = function () {
            const {data} = $scope;
            if(!service){
                return;
            }
            const regex = /(post|POST|put|PUT|patch|PATCH)[:](.+$)/;
            if (!regex.test(service)){
                return;
            }
            const si = regex.exec(service);
            const method = si[1];
            const url = si[2];
            console.log('提交？',data,this);
            $.ajax({
                type: method,
                url: url,
                contentType:function(){
                    return 'application/json;charset=UTF-8'
                }(),
                data: function () {
                    return JSON.stringify(data);
                }(),
                success: function(resp){
                    const {code,msg,data} = resp;
                    if (code === 0) {
                        $ctrl.cancel();
                        window.location.reload(true);
                    }else {
                        alert(`操作失败: ${msg}`);
                    }
                }
            });
        }
        // console.log('模态框示例')

    });
    m.directive('tableEditor',function () {
        return {
            controller:function ($scope, $attrs) {
            },
            link:function (scope, element, attrs) {
                // console.log('编辑器link.',scope);
                function doClick(){
                    // console.log(scope)
                }
                element.bind('click', function (e) {
                    doClick();
                });
            }
        }
    });
    m.directive('tableAddButton',function () {
        return {
            template:function($element, $attrs){
                const {title} = $attrs;
                return [
                    '<button class="btn btn-space btn-primary">',
                    title,
                    '</button>'
                ].join('');
            },
            scope: false,
            controller:function ($scope, $attrs) {
            },
            link:function (scope, element, attrs) {
                // console.log('编辑器link.',scope);
                function doClick(){
                    scope.open();
                    // console.log('表格点击',scope);
                }
                element.bind('click', function (e) {
                    doClick();
                });
            }
        }
    });
    m.directive('targetModal',function () {
        return {
            controller:function ($scope, $attrs) {
                // console.log('targetModal',$attrs,$scope);
            },
            link:function (scope, element, attrs) {
                function doClick(){
                    const {targetModal} = attrs;
                    const {open} = scope[targetModal];
                    open(scope);
                }
                element.bind('click', function (e) {
                    doClick();
                });
            }
        }
    });
    m.directive('modalFormView',function () {
        return {
            // scope: false,

            controller:function ($scope, $attrs,$uibModal) {
                // console.log()
                const {modalFormView,id,service} = $attrs;
                // $scope.service = service;
                $scope[modalFormView] = {};
                $scope[modalFormView].open = function(scope){
                    const {item} = scope;
                    console.log('弹窗打开？',item);
                    const modalInstance = $uibModal.open({
                        animation:false,
                        windowClass:'colored-header colored-header-primary',
                        backdropClass:'fade show',
                        templateUrl:id,
                        controller:'modalInstanceCtrl',
                        controllerAs:'$ctrl',
                        resolve: {
                            form: function () {
                                return {
                                    data:item,
                                    service:service
                                };
                            }
                        }
                    });
                };
            }
        }
    });
    console.log('表格流');
}(angular);